<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <!--
  Copyright 2013 The Chromium Authors. All rights reserved.
  Use of this source code is governed by a BSD-style license that can be
  found in the LICENSE file.
  -->
  <head>
    <META HTTP-EQUIV="Pragma" CONTENT="no-cache" />
    <META HTTP-EQUIV="Expires" CONTENT="-1" />
    <script type="text/javascript" src="nacltest.js"> </script>
    <script type="text/javascript" src="ppapi_bad.js"> </script>
    <title> PPAPI bad manifest/nexe URLs </title>
    <style type="text/css">
    .naclModule { background-color: gray; margin: 2px 2px; }
    </style>
  </head>
<body id="body">
<script type="text/javascript">
//<![CDATA[

var prefix = 'NaCl module load failed: ';

// PNaCl may have slightly different error messages (pexe instead of nexe).
function couldNotAccessNexe(is_pnacl) {
  if (!is_pnacl) {
    return prefix + 'access to nexe url was denied.';
  } else {
    return prefix + 'PnaclCoordinator: pexe load failed (no access).';
  }
}

// PNaCl may have slightly different error messages (pexe instead of nexe).
function couldNotLoadNexe(is_pnacl) {
  if (!is_pnacl) {
    return prefix + 'could not load nexe url.';
  } else {
    /* PP_ERROR_FAILED */
    return prefix + 'PnaclCoordinator: pexe load failed (pp_error=-2).';
  }
}

function declareTests(tester) {
  var is_pnacl = getTestArguments()['pnacl'] !== undefined;
  var mime_type = "application/x-nacl";
  if (is_pnacl) {
    mime_type = "application/x-pnacl";
  }

  // 'bad_magic' loads a manifest, then loads a nexe that tests as invalid.
  badLoadTest(
      tester,
      'bad_magic',
      'ppapi_bad_magic.nmf',
      mime_type,
      is_pnacl
          // Use a regular expression here: the translator outputs debug
          // information in the error message.
        ? new RegExp('NaCl module load failed: PnaclCoordinator: ' +
                     'PNaCl Translator Error: .*' +
                     'Invalid PNaCl bitcode header')
        : 'NaCl module load failed: Bad ELF header magic number');

  // 'nonexistent_nexe' loads a manifest, then tries to load a nonexistent nexe.
  badLoadTest(
      tester,
      'nonexistent_nexe',
      'ppapi_bad_doesnotexist.nmf',
      mime_type,
      couldNotLoadNexe(is_pnacl));

  // 'nonexistent_manifest' tries to load a nonexistent manifest.
  badLoadTest(
      tester,
      'nonexistent_manifest',
      'doesnotexist.manifest',
      mime_type,
      'NaCl module load failed: could not load manifest url.');

  // 'bad_manifest' loads an invalid manifest.
  badLoadTest(
      tester,
      'bad_manifest',
      'ppapi_bad.html',
      mime_type,
      new RegExp('NaCl module load failed: manifest JSON parsing failed: ' +
                 'Line: 1, column: 1.*'));

  // 'bad_manifest_uses_nexes' loads a manifest with an obsolete 'nexes' section.
  badLoadTest(
      tester,
      'bad_manifest_uses_nexes',
      'ppapi_bad_manifest_uses_nexes.nmf',
      mime_type,
      'NaCl module load failed: manifest: missing \'program\' section.');

  // 'bad_manifest_bad_files' loads a manifest with a bad 'files' section.
  badLoadTest(
      tester,
      'bad_manifest_bad_files',
      'ppapi_bad_manifest_bad_files.nmf',
      mime_type,
      // Manifest loader expects either 'url' or 'pnacl-translate' key present.
      // If neither is found, it complains about the last one.
      'NaCl module load failed: manifest: file.txt property \'unknown_arch\' does not have required key: \'url\'.');

  // 'bad_manifest_nexe_arch' loads a manifest with no program entry for the
  // user's architecture
  badLoadTest(
      tester,
      'bad_manifest_nexe_arch',
      'ppapi_bad_manifest_nexe_arch.nmf',
      mime_type,
      is_pnacl
        ? 'NaCl module load failed: manifest: no version of program given for portable.'
        : 'NaCl module load failed: manifest: no version of program given for current arch and no portable version found.');

  //////////////////////////////////////
  // Initialization errors begin here //
  //////////////////////////////////////

  // 'bad_ppp_initialize' loads a manifest, then loads a nexe that fails to
  // initialize PPAPI module
  badLoadTest(
      tester,
      'bad_ppp_initialize',
      'ppapi_bad_ppp_initialize.nmf',
      mime_type,
      'NaCl module load failed: Nexe crashed during startup');

  // 'bad_ppp_initialize_crash' loads a manifest, then loads a nexe that crashes
  // before initializing PPAPI module
  badLoadTest(
      tester,
      'bad_ppp_initialize_crash',
      'ppapi_bad_ppp_initialize_crash.nmf',
      mime_type,
      'NaCl module load failed: Nexe crashed during startup');

  // 'bad_no_ppp_instance' loads a manifest, then loads a nexe that fails to
  // get the required PPP_Instance interface
  badLoadTest(
      tester,
      'bad_no_ppp_instance',
      'ppapi_bad_no_ppp_instance.nmf',
      mime_type,
      'NaCl module load failed: could not initialize module.');

  // 'bad_get_ppp_instance_crash' loads a manifest, then loads a nexe that
  // crashes when getting the required PPP_Instance interface
  badLoadTest(
      tester,
      'bad_get_ppp_instance_crash',
      'ppapi_bad_get_ppp_instance_crash.nmf',
      mime_type,
      'NaCl module load failed: could not initialize module.');

  // 'bad_ppp_instance_didcreate' loads a manifest, then loads a nexe that fails
  // to create the instance
  badLoadTest(
      tester,
      'bad_ppp_instance_didcreate',
      'ppapi_bad_ppp_instance_didcreate.nmf',
      mime_type,
      'NaCl module load failed: could not create instance.');

  // 'bad_ppp_instance_didcreate_crash' loads a manifest, then loads a nexe that
  // crashes before creating the instance.
  badLoadTest(
      tester,
      'bad_ppp_instance_didcreate_crash',
      'ppapi_bad_ppp_instance_didcreate_crash.nmf',
      mime_type,
      'NaCl module load failed: could not create instance.');

/* TODO(bbudge) Re-enable this test when the IPC proxy can report these errors.
  http://crbug.com/160076
  // 'bad_event_replay_crash' loads a manifest, then loads a nexe and replays
  // the events that occured during loading causing the nexe to crash before
  // proxy start-up was completed.
  badLoadTest(
      tester,
      'bad_event_replay_crash',
      'ppapi_bad_event_replay_crash.nmf',
      mime_type,
      'NaCl module load failed: instance crashed after creation.');
*/
}


// The driver invoked when the body has finished loading.
function runTests() {
  var tester = new Tester($('body'));
  tester.loadErrorsAreOK();
  declareTests(tester);
  tester.run();
}
//]]>
</script>

<!-- The tests will create and remove embeds from this div. -->
<div id="embeds"></div>

<script type="text/javascript">
//<![CDATA[
runTests();
//]]>
</script>
</body>
</html>
